package edu.wust.homework.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.wust.homework.entity.Student;
import edu.wust.homework.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private IStudentService studentService;
    @PostMapping("/login")
    public String login(@RequestParam("userName") String userName,
    @RequestParam("password")String password){
        System.out.println("userName:"+userName+"password:"+password);
        //业务逻辑进行调用持久层
        Student s=studentService.getStuByUserName(userName);
        if (s != null && s.getPassword().equals(password)) {
            return "login success";
        } else {
            return "login error，userName or password is wrong！";
        }
    }

// http://127.0.0.1:8080/stu/reg
//{
//    "userName": "249641",
//        "password": "123456",
//        "name": "dcl",
//        "sex": "男",
//        "age": 22,
//        "phone": "1234567890"
//}

    @PostMapping("/reg")
    public String register(@RequestBody Student s){
        class RegistrationStatus {
            public static final int SUCCESS = 1;
            public static final int USERNAME_DUPLICATE = -1;
            public static final int INSERT_FAILED = 0;
        }

        int registrationStatus  =studentService.register(s);

        return switch (registrationStatus) {
            case RegistrationStatus.SUCCESS -> "register succeed";
            case RegistrationStatus.USERNAME_DUPLICATE -> "register failed, userName is duplicated";
            case RegistrationStatus.INSERT_FAILED -> "register failed, insert failed";
            default -> "unknown registration status";
        };
    }

    @PostMapping("/modify")
    public String modify(@RequestBody Map<String, String> requestParams){
        String userName = requestParams.get("userName");
        String oldPassword = requestParams.get("oldPassword");
        String newPassword = requestParams.get("newPassword");

        int ok =studentService.modifyPassword(userName,oldPassword,newPassword);
        return ok==1?"modify password succeed":"modify password failed";
    }



    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        int rowsAffected = studentService.deleteById(id);

        return rowsAffected>0?"delete succeed":"delete failed";
    }

//    http://127.0.0.1:8080/stu/deleteByUserName?userName=newstudent5
    @DeleteMapping("/deleteByUserName")
    public String deleteByUserName(@RequestParam String userName){
        int rowsAffected = studentService.deleteByUsername(userName);
        return rowsAffected > 0? "delete succeed" : "delete failed";
    }

    @GetMapping("/getAllStudentsByPage")
    public List<Student> getAllStudentsByPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "5") long size
    ) {
        return studentService.getAllStudentsByPage(current, size);
    }


    //查询全部学生
    @GetMapping("/selectAll")
    public List<Student>selectAll(){
        return studentService.getAllStudents();
    }

//    {
//        "userName":"249641",
//            "name":"张三",
//            "age":18,
//            "sex":"男",
//            "phone":"18245"
//    }
    @PutMapping("/updateStudentInfo")
    public ResponseEntity<String> updateStudentInfo(@RequestBody Student student) {
        if (studentService.updateStudentInfo(student)) {
            return ResponseEntity.ok("User information updated successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update user information.");
        }
    }






}
